﻿using Microsoft.EntityFrameworkCore;
using Naruto.Repository;
using Naruto.TaskManagement.Entitys;
using Naruto.TaskManagement.Entitys.Models;
using Naruto.TaskManagement.Entitys.VM;
using Naruto.TaskManagement.IRepository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Naruto.TaskManagement.Repository
{
    /// <summary>
    /// 用户操作仓储
    /// </summary>
    public class UserRepository : IUserRepository
    {

        private readonly IRepository<AuthCenterContext> repository;


        public UserRepository(IRepository<AuthCenterContext> _repository)
        {
            repository = _repository;
        }
        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public async Task<UserInfoViewModel> GetUserInfoByUserName(string userName)
        {
            var userQueryable = await repository.QueryAsync<User>();
            return await userQueryable.Where(a => a.UserName == userName).AsNoTracking()
                   .Select<User, UserInfoViewModel>().FirstOrDefaultAsync();
        }

        /// <summary>
        /// 获取用户的邮箱
        /// </summary>
        /// <returns></returns>
        public async Task<List<string>> GetUserEmail()
        {
            var userQueryable = await repository.QueryAsync<User>();
            return await userQueryable.AsNoTracking().Select(a => a.Email).ToListAsync();
        }

        /// <summary>
        /// 根据用户id 获取用户信息
        /// </summary>
        /// <param name="userId">用户id</param>
        /// <returns></returns>
        public async Task<GetUserInfoByIdViewModel> GetUserInfoById(string userId)
        {
            var userQueryable = await repository.QueryAsync<User>();
            return await userQueryable.Where(a => a.Id == userId).AsNoTracking().Select<User, GetUserInfoByIdViewModel>().FirstOrDefaultAsync();
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="pswd"></param>
        /// <returns></returns>
        public async Task UpdatePasswordByUserId(string userId, string pswd)
        {
            await repository.Command<User>()
                   .UpdateAsync(a => a.Id == userId,
                   entity =>
                   {
                       entity.Password = pswd;
                       return entity;
                   });
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="pswd"></param>
        /// <returns></returns>
        public async Task UpdatePasswordByEmail(string email, string pswd)
        {
            await repository.Command<User>()
                   .UpdateAsync(a => a.Email == email,
                   entity =>
                   {
                       entity.Password = pswd;
                       return entity;
                   });
        }

        /// <summary>
        /// 验证用户名是否存在
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public async Task<bool> ExistsUserName(string userName)
        {
            var userQueryable = await repository.QueryAsync<User>();
            return await userQueryable.Where(a => a.UserName == userName).AsNoTracking().AnyAsync();
        }
        /// <summary>
        /// 验证邮件是否存在
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public async Task<bool> ExistsEmail(string email)
        {
            var userQueryable = await repository.QueryAsync<User>();
            return await userQueryable.Where(a => a.Email == email).AsNoTracking().AnyAsync();
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public async Task Add(User info)
        {
            await repository.Command<User>().AddAsync(info);
        }

        /// <summary>
        /// 根据组织获取用户信息
        /// </summary>
        /// <param name="oid"></param>
        /// <returns></returns>
        public async Task<List<GetUserByOrganizationIdViewModel>> GetUserByOrganizationId(string oid)
        {
            var userQueryable = await repository.QueryAsync<User>();
            return await userQueryable.Where(a => a.OrganizationId == oid && a.Vaild == 1).AsNoTracking().Select<User, GetUserByOrganizationIdViewModel>().ToListAsync();
        }
        /// <summary>
        /// 获取邮箱 通过用户名
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public async Task<string> GetEmailByUserName(string userName)
        {
            var userQueryable = await repository.QueryAsync<User>();
            return await userQueryable.Where(a => a.UserName == userName && a.Vaild == 1).AsNoTracking().Select(a => a.Email).FirstOrDefaultAsync();
        }

        /// <summary>
        /// 根据组织id 获取用户信息
        /// </summary>
        /// <param name="oid"></param>
        /// <returns></returns>
        public async Task<List<string>> GetEmailByOrganizationId(string oid)
        {
            var userQueryable = await repository.QueryAsync<User>();
            return await userQueryable.Where(a => a.OrganizationId == oid && a.Vaild == 1).AsNoTracking().Select(a => a.Email).ToListAsync();
        }

        /// <summary>
        /// 获取用户的组织id
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public async Task<string> GetUserOrganizationId(string userName)
        {
            var userQueryable = await repository.QueryAsync<User>();
            return await userQueryable.Where(a => a.UserName == userName && a.Vaild == 1).AsNoTracking().Select(a => a.OrganizationId).FirstOrDefaultAsync();
        }

        /// <summary>
        /// 获取用户的名称
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public async Task<string> GetName(string userName)
        {
            var userQueryable = await repository.QueryAsync<User>();
            return await userQueryable.Where(a => a.UserName == userName && a.Vaild == 1).AsNoTracking().Select(a => a.Name).FirstOrDefaultAsync();
        }


        /// <summary>
        /// 根据用户名更新组织id
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public async Task UpdateOrganizationIdByUserName(string userName, string organizationId)
        {
            await repository.Command<User>()
               .UpdateAsync(a => a.UserName == userName, entity =>
               {
                   entity.OrganizationId = organizationId;
                   return entity;
               });
        }

        /// <summary>
        /// 获取组织下的用户信息
        /// </summary>
        /// <param name="oid"></param>
        /// <returns></returns>
        public async Task<List<GetOrganizationIdUserViewModel>> GetOrganizationIdUser(string oid)
        {
            var userQueryable = await repository.QueryAsync<User>();
            return await userQueryable.Where(a => a.OrganizationId == oid && a.Vaild == 1).AsNoTracking().Select<User, GetOrganizationIdUserViewModel>().ToListAsync();
        }

        /// <summary>
        /// 获取用户的基本信息
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public async Task<GetUserBaseInfoByIdViewModel> GetUserBaseInfoById(string userId)
        {
            var userQueryable = await repository.QueryAsync<User>();
            return await userQueryable.Where(a => a.Id == userId && a.Vaild == 1).AsNoTracking().Select<User, GetUserBaseInfoByIdViewModel>().FirstOrDefaultAsync();
        }

        /// <summary>
        /// 获取用户的头像
        /// </summary>
        /// <param name="userIdList"></param>
        /// <returns></returns>
        public async Task<List<GetHeardImgUrlNameByUserIdListViewModel>> GetHeardImgUrlNameByUserIdList(List<string> userIdList)
        {
            var userQueryable = await repository.QueryAsync<User>();
            return await userQueryable.Where(a => userIdList.Contains(a.Id) && a.Vaild == 1).AsNoTracking().Select<User, GetHeardImgUrlNameByUserIdListViewModel>().ToListAsync();
        }

        /// <summary>
        /// 根据用户id 获取 用户信息
        /// </summary>
        /// <param name="userId">用户id</param>
        /// <returns></returns>
        public async Task<GetUserInfoViewModel> GetUserInfoByUserId(string userId)
        {
            var userQueryable = await repository.QueryAsync<User>();
            return await userQueryable.Where(a => a.Id == userId && a.Vaild == 1).AsNoTracking().Select<User, GetUserInfoViewModel>().FirstOrDefaultAsync();
        }
    }
}
